<?php
/**
* Class Translation into Bo Controller
* @author SillySmart
* @copyright SillySmart
* @package Mvc.Controllers.Bo
* @see Mvc.Controllers.Bo.ControllerProtected
* @see Mvc.Controllers.SiteProtected
* @see Sls.Controllers.Core.SLS_GenericController
* @since 1.0
*
*/
class BoTranslation extends BoControllerProtected
{
	public function init()
	{
		parent::init();
	}

	public function action()
	{
		$xml = $this->getXML();
		$boActions = SLS_BoRights::getDistinctActions();
		$xml->startTag("bo_actions");
		for($i=0 ; $i<$count=count($boActions) ; $i++)
		{
			$xml->startTag("bo_action");
				foreach($boActions[$i] as $key => $value)
					$xml->addFullTag($key,$value,true);
			$xml->endTag("bo_action");
		}
		$xml->endTag("bo_actions");

		// Check account rights
		$right = SLS_BoRights::isAuthorized("custom","",$this->_generic->getActionId());
		if ($right == -1)
			$this->_generic->redirect("Bo/Connection.sls");
		else if ($right == 0)
			$this->_generic->redirect("Bo/Connection.sls");
		// /Check account rights

		set_time_limit(0);
		$xmlController = $this->_generic->getControllersXML();

		// Langs
		$langObject = $this->_generic->getObjectLang();
		$langs = $langObject->getSiteLangs();

		$xml->startTag("langs");

		// Check reload
		if ($this->_http->getParam("reload_translate") == "true")
		{
			// Get the target
			$target_id = $this->_http->getParam("target_id");
			$xml->addFullTag("target_id",$target_id,true);

			// Init source file
			$fileName = "";

			// If target_id map to site
			if ($target_id == "site")
				$fileName = $this->_generic->getPathConfig("genericLangs")."site";
			// Else if map to controller
			else if (SLS_String::startsWith($target_id,"c_"))
			{
				// Try to get the controller
				$result = array_shift($xmlController->getTagsAttributes("//controllers/controller[@id='".$target_id."']",array("name")));
				if (!empty($result["attributes"][0]["value"]))
					$fileName = $this->_generic->getPathConfig("actionLangs").$result["attributes"][0]["value"]."/__".$result["attributes"][0]["value"];
			}
			// Else if map to action
			else if (SLS_String::startsWith($target_id,"a_"))
			{
				// Try to get the controller
				$result = array_shift($xmlController->getTagsAttributes("//controllers/controller[scontrollers/scontroller[@id='".$target_id."']]",array("name")));
				if (!empty($result["attributes"][0]["value"]))
				{
					$controllerName = $result["attributes"][0]["value"];
					// Try to get the action
					$result = array_shift($xmlController->getTagsAttributes("//controllers/controller/scontrollers/scontroller[@id='".$target_id."']",array("name")));
					if (!empty($result["attributes"][0]["value"]))
						$fileName = $this->_generic->getPathConfig("actionLangs").$controllerName."/".$result["attributes"][0]["value"];
				}
			}

			// If we've found a source file
			if (!empty($fileName))
			{
				$properties = array();
				$params = $this->_http->getParams();
				foreach($params as $key => $value)
					$params[$key] = SLS_String::trimSlashesFromString($value);

				foreach($langs as $lang)
					$properties[$lang] = array();

				foreach($params as $key => $value)
					if (SLS_String::startsWith($key,"LG_"))
						foreach($langs as $lang)
							if (SLS_String::startsWith($key,"LG_".$lang."_"))
								$properties[$lang][SLS_String::substrAfterFirstDelimiter($key,"LG_".$lang."_")] = $value;
				$this->setLangProperties($fileName,$properties,$langs);
			}
		}

		// Load Generics Langs
		$xml->startTag("site");
		$properties = $this->getLangProperties($this->_generic->getPathConfig("genericLangs")."site",$langs);
		$xml->startTag("properties");
		foreach($properties as $key => $value)
		{
			$xml->startTag("property");
			$xml->addFullTag("name",$key,true);
			$xml->startTag("values");
			foreach($langs as $lang)
			{
				$xml->startTag("value");
				$xml->addFullTag("name",str_replace('\"','"',$value[$lang]),true);
				$xml->addFullTag("lang",$lang,true);
				$xml->addFullTag("type",(strlen(str_replace('\"','"',$value[$lang]))>50) ? "textarea" : "input",true);
				$xml->endTag("value");
			}
			$xml->endTag("values");
			$xml->endTag("property");
		}
		$xml->endTag("properties");
		$xml->endTag("site");

		// Load Controller Lang
		$controllers = $xmlController->getTagsAttributes("//controllers/controller[@side='user' and count(@isBo) = 0]",array("name","id"));
		$xml->startTag("controllers");
		for($i= 0 ; $i<$count=count($controllers) ; $i++)
		{
			$xml->startTag("controller");
			$xml->addFullTag("name",$controllers[$i]["attributes"][0]["value"],true);
			$xml->addFullTag("id",$controllers[$i]["attributes"][1]["value"],true);
			$properties = $this->getLangProperties($this->_generic->getPathConfig("actionLangs").$controllers[$i]["attributes"][0]["value"]."/__".$controllers[$i]["attributes"][0]["value"],$langs);
			$xml->startTag("properties");
			foreach($properties as $key => $value)
			{
				$xml->startTag("property");
				$xml->addFullTag("name",$key,true);
				$xml->startTag("values");
				foreach($langs as $lang)
				{
					$xml->startTag("value");
					$xml->addFullTag("name",str_replace('\"','"',$value[$lang]),true);
					$xml->addFullTag("lang",$lang,true);
					$xml->addFullTag("type",(strlen(str_replace('\"','"',$value[$lang]))>50) ? "textarea" : "input",true);
					$xml->endTag("value");
				}
				$xml->endTag("values");
				$xml->endTag("property");
			}
			$xml->endTag("properties");

			$actions = $xmlController->getTagsAttributes("//controllers/controller[@name='".$controllers[$i]["attributes"][0]["value"]."']/scontrollers/scontroller",array("name","id"));
			$xml->startTag("actions");
			for($j= 0 ; $j<$countJ=count($actions) ; $j++)
			{
				$xml->startTag("action");
				$xml->addFullTag("name",$actions[$j]["attributes"][0]["value"],true);
				$xml->addFullTag("id",$actions[$j]["attributes"][1]["value"],true);
				$properties = $this->getLangProperties($this->_generic->getPathConfig("actionLangs").$controllers[$i]["attributes"][0]["value"]."/".$actions[$j]["attributes"][0]["value"],$langs);
				$xml->startTag("properties");
				foreach($properties as $key => $value)
				{
					$xml->startTag("property");
					$xml->addFullTag("name",$key,true);
					$xml->startTag("values");
					foreach($langs as $lang)
					{
						$xml->startTag("value");
						$xml->addFullTag("name",str_replace('\"','"',$value[$lang]),true);
						$xml->addFullTag("lang",$lang,true);
						$xml->addFullTag("type",(strlen(str_replace('\"','"',$value[$lang]))>50) ? "textarea" : "input",true);
						$xml->endTag("value");
					}
					$xml->endTag("values");
					$xml->endTag("property");
				}
				$xml->endTag("properties");
				$xml->endTag("action");
			}
			$xml->endTag("actions");

			$xml->endTag("controller");
		}
		$xml->endTag("controllers");

		$xml->endTag("langs");
		$this->saveXML($xml);
	}

	public function getLangProperties($file,$langs)
	{
		$properties = array();

		foreach($langs as $lang)
		{
			$fileN = $file.".".$lang.".lang.php";
			$properties_{$lang} = array();

			if (file_exists($fileN))
			{
				$fileExploded = explode("\n",file_get_contents($fileN));
				array_map("trim",$fileExploded);

				foreach($fileExploded as $line)
				{
					$line = trim($line);
					if (SLS_String::startsWith($line,'$GLOBALS[$GLOBALS[\'PROJECT_NAME\']]'))
					{
						$type = (SLS_String::startsWith($line,'$GLOBALS[$GLOBALS[\'PROJECT_NAME\']][\'XSL\'][\'')) ? "XSL" : "JS";
						$end = SLS_String::substrAfterFirstDelimiter($line,'$GLOBALS[$GLOBALS[\'PROJECT_NAME\']][\''.$type.'\'][\'');

						$key = SLS_String::substrBeforeFirstDelimiter($end,'\']');
						$value = SLS_String::substrAfterFirstDelimiter(SLS_String::substrBeforeLastDelimiter($end,'";'),'"');
						$properties_{$lang}[$key] = $value;
					}
				}
			}
		}

		foreach($langs as $lang)
			foreach($properties_{$lang} as $key => $value)
				$properties[$key][$lang] = $value;

		return $properties;
	}

	public function setLangProperties($file,$properties,$langs)
	{
		foreach($langs as $lang)
		{
			$fileN = $file.".".$lang.".lang.php";

			if (file_exists($fileN))
			{
				$newFile = "";
				$fileExploded = explode("\n",file_get_contents($fileN));
				array_map("trim",$fileExploded);
				$endFound = false;

				foreach($fileExploded as $line)
				{
					if (!$endFound)
					{
						$line = trim($line);

						if (SLS_String::startsWith($line,'?>'))
							$endFound = true;

						if (SLS_String::startsWith($line,'$GLOBALS[$GLOBALS[\'PROJECT_NAME\']]'))
						{
							$type = (SLS_String::startsWith($line,'$GLOBALS[$GLOBALS[\'PROJECT_NAME\']][\'XSL\'][\'')) ? "XSL" : "JS";
							$end = SLS_String::substrAfterFirstDelimiter($line,'$GLOBALS[$GLOBALS[\'PROJECT_NAME\']][\''.$type.'\'][\'');
							$key = SLS_String::substrBeforeFirstDelimiter($end,'\']');

							if (!empty($properties[$lang][$key]))
								$newFile .= '$GLOBALS[$GLOBALS[\'PROJECT_NAME\']][\''.$type.'\'][\''.$key.'\'] = "'.str_replace('"','\"',$properties[$lang][$key]).'";';
							else
								$newFile .= $line;
						}
						else
							$newFile .= $line;

						if (!$endFound)
							$newFile .= "\n";
					}
				}
				file_put_contents($fileN,$newFile);
			}
		}
	}
}
?>